<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="4" :xs="24" v-loading="typeLoading">
        <div class="head-container">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd('type', 'no')"
            v-hasPermi="['system:solution:add']" style="width: 100%">
            新增一级分类
          </el-button>
          <el-input v-model="solutionName" placeholder="请输入分类名称" clearable size="small" prefix-icon="el-icon-search"
            style="margin-bottom: 20px" />
        </div>
        <div class="head-container">
          <el-tree :data="solutionList" :props="solutionProps" :expand-on-click-node="false"
            :filter-node-method="filterNode" ref="tree" node-key="solutionId" default-expand-all highlight-current
            @node-click="handleNodeClick">
            <div class="custom-tree-node" slot-scope="{ node, data }">
              <div class="fwrap">{{ node.label }}</div>
              <div>
                <el-button type="text" size="mini" v-hasPermi="['system:solution:add']"
                  @click.stop="handleAdd('type', 'have', data)" v-if="data.parentId == 0 || data.parentId == null">
                  新增
                </el-button>
                <el-button type="text" size="mini" v-hasPermi="['system:solution:edit']"
                  @click.stop="handleUpdate('type', data.parentId == 0 || data.parentId == null ? 'no' : 'have', data)">
                  编辑
                </el-button>
                <el-button type="text" size="mini" v-hasPermi="['system:solution:remove']"
                  @click.stop="handleDelete('type', data)">
                  删除
                </el-button>
              </div>
            </div>
          </el-tree>
        </div>
      </el-col>
      <el-col :span="20" :xs="24">
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd('content', 'no')"
              v-hasPermi="['system:solution:add']" v-if="parentId != '0' && parentId != null">
              新增
            </el-button>
          </el-col>
        </el-row>
        <el-table v-loading="loading" border :data="dataList">
          <el-table-column label="方案名称" align="center" prop="solutionName" show-overflow-tooltip />
          <el-table-column label="排序" align="center" prop="sort" />
          <el-table-column label="操作" align="center" width="180">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate('content', 'no', scope.row)"
                v-hasPermi="['system:solution:edit']">
                修改
              </el-button>
              <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete('content', scope.row)"
                v-hasPermi="['system:solution:remove']">
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
          @pagination="getList" />
      </el-col>
    </el-row>

    <!-- 添加或修改解决方案对话框 -->
    <el-dialog :title="form.solutionId ? '编辑' : '新增'" :visible.sync="open" width="50%" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="方案名称" prop="solutionName">
          <el-input v-model="form.solutionName" maxlength="30" placeholder="请输入方案名称" />
        </el-form-item>
        <el-form-item label="图片" prop="imgUrl" v-if="imgshow">
          <!-- <el-input v-model="form.imgUrl" /> -->
          <Image-upload v-model="form.imgUrl" :limit="1" :isShowTip="false" />
        </el-form-item>
        <el-form-item label="方案内容" v-if="form.parentId != 0" prop="content">
          <Editors :content="form.content" v-model="form.content" />
        </el-form-item>
        <el-form-item label="排序" v-if="actionType == 'content'" prop="sort">
          <el-input v-model.number="form.sort" type="number" placeholder="请输入排序" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listSolution,
  listSolutionType,
  delSolution,
  addSolution,
  updateSolution,
} from "@/api/system/solution";

export default {
  name: "Solution",
  data() {
    return {
      typeLoading: false, // 类型加载
      loading: false, // 遮罩层
      total: 0, // 总条数
      solutionList: [], // 解决方案列表
      dataList: [], // 方案数据列表
      solutionProps: {
        children: "children",
        label: "solutionName",
      },
      solutionName: null, // 过滤方案名称
      actionType: "type", // 表单类型
      solutionId: 0, // 方案id
      parentId: 0, // 父级id
      open: false, // 是否显示弹出层
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      form: {}, // 表单参数
      rules: {
        sort: [{ pattern: /^[1-9]\d*$/, message: "请输入正整数", trigger: "blur" }],
      }, // 表单校验
      imgshow: false,//是否上传图片
    };
  },
  created() {
    this.getTypeList();
  },
  watch: {
    // 根据名称筛选
    solutionName(val) {
      this.$refs.tree.filter(val);
    },
  },
  methods: {
    /** 查询解决方案列表 */
    getTypeList() {
      this.typeLoading = true;
      listSolutionType()
        .then((res) => {
          this.solutionList = res.data;
        })
        .finally(() => {
          this.typeLoading = false;
        });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.solutionName.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.solutionId = data.solutionId;
      this.parentId = data.parentId;
      this.handleQuery();
    },
    // 获取分类下帮助列表
    getList() {
      this.loading = true;
      const data = {
        ...this.queryParams,
        parentId: this.solutionId
      }
      listSolution(data)
        .then((res) => {
          this.dataList = res.rows;
          this.total = res.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 新增类型按钮
    handleAdd(type, img, row) {
      this.reset();
      if (type == "type") {
        this.form.parentId = row ? row.solutionId : "0";
      } else {
        this.form.parentId = this.solutionId;
      }
      if (img === 'have') {
        this.imgshow = true;
      } else {
        this.imgshow = false;
      }
      this.actionType = type;
      this.open = true;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        solutionId: null,
        solutionName: null,
        parentId: 0,
        content: "",
        sort: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 修改按钮操作 */
    handleUpdate(type, img, row) {
      if (img === 'have') {
        this.imgshow = true;
      } else {
        this.imgshow = false;
      }
      this.form = Object.assign({}, row);
      this.actionType = type;
      this.solutionId = row.solutionId;
      this.parentId = row.parentId;
      this.open = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.solutionId != null) {
            updateSolution(this.form).then((res) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              if (this.actionType == "type") {
                this.getTypeList();
              } else {
                this.solutionId = this.parentId;
                this.getList();
              }
            });
          } else {
            addSolution(this.form).then((res) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              if (this.actionType == "type") {
                this.getTypeList();
              } else {
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(type, row) {
      this.actionType = type;
      this.$modal
        .confirm("是否确认删除名为【" + row.solutionName + "】的数据项？")
        .then(() => {
          return delSolution([row.solutionId]).then(() => {
            this.$modal.msgSuccess("删除成功");
            if (this.actionType == "type") {
              this.getTypeList();
            } else {
              this.getList();
            }
          });
        })
        .catch(() => { });
    },
  },
};
</script>
<style lang="scss" scoped>
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;

  .fwrap {
    max-width: 130px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}
</style>
